<template>
  <div class="login-container">
    <div class="login-page">
    <el-form :model="ruleForm2" :rules="rules2"
             status-icon
             ref="ruleForm2"
             label-position="left"
             label-width="0px"
             class="demo-ruleForm">
      <h3 class="title">忘记密码</h3>

      <el-form-item prop="email">
        <el-input type="text"
                  v-model="ruleForm2.email"
                  auto-complete="off"
                  placeholder="请输入注册邮箱"
                  class="input-with-send">
          <el-button slot="append" icon="el-icon-s-promotion" @click="sendEmail"></el-button>
        </el-input>
      </el-form-item>

      <el-form-item prop="code" >
        <el-input type="text"
                  v-model="ruleForm2.code"
                  auto-complete="off"
                  placeholder="请输入验证码"
        ></el-input>
      </el-form-item>

      <el-form-item prop="password">
        <el-input type="text"
                  v-model="ruleForm2.password"
                  auto-complete="off"
                  placeholder="请输入新密码"
        ></el-input>
      </el-form-item>

      <div v-if="hadSend">

      </div>
      <el-form-item>
        <el-button type="primary" @click="handleSubmit" :loading="logining">修改</el-button>
      </el-form-item>
    </el-form>
  </div>
  </div>
</template>

<script>
  import {sendPwdChangeEmail , updatePwd} from '@/api/user'
      export default {
      name: "forget",
      data(){
        return {
          hadSend:true,
          active: 0,
          logining: false,
          ruleForm2: {
            email : '',
            code : '',
          },
          rules2: {
            email: [{required: true, message: '请输入邮箱', trigger: 'blur'}],
            code: [{required: true, message: '请输入验证码', trigger: 'blur'}],
            password: [{required: true, message: '请输入新密码', trigger: 'blur'}],
          },
          checked: false
        }
      },
      methods: {
        next() {
          if (this.active++ > 2) this.active = 0;
        },
        sendEmail(){
          sendPwdChangeEmail(this.ruleForm2).then(() => {
            this.hadSend=false;
            this.$message({
              message: '发送成功，清前往邮箱查看验证码',
              type: 'success'
            });
          }).catch((response) => {
            console.log(response);
          })
        },
        handleSubmit(event){
          // 校验重复密码是否重复
          this.$refs.ruleForm2.validate((valid) => {
            if (valid) {
              updatePwd(this.ruleForm2).then(() => {
                  this.$message({
                    message: '修改成功',
                    type: 'success'
                  });
                this.$router.push( '/app/user/login');
                }).catch((response) => {
                  console.log(response);
                })
            } else {
              console.log('error submit!!')
              return false
            }
          })
        }
      },
    }
</script>

<style scoped>
  .title {
    font-size: 26px;
    color: #9aaabf;
    margin: 0px auto 40px auto;
    font-weight: bold;
  }
  .login-container {
    display: table;
    height: 100%;
    width: 100%;
  }
  .login-page {
    display: table-cell;
    vertical-align: middle;
    text-align: center;

  }
  .demo-ruleForm{
    -webkit-border-radius: 5px;
    border-radius: 5px;
    padding: 35px 35px 15px;
    background: #fff;
    border: 1px solid #eaeaea;
    box-shadow: 0 0 25px #cac6c6;
  }
  label.el-checkbox.rememberme {
    margin: 0px 0px 15px;
    text-align: left;
  }

</style>
